package com.deerblock.config;

import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.util.TypedValue;
import android.view.Surface;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.deerblock.R;
import com.deerblock.utils.AppUtil;
import com.umeng.umverify.UMVerifyHelper;
import com.deerblock.Constant;




public abstract class BaseUIConfig implements AuthPageConfig {
  public Activity mActivity;
  public Context mContext;
  public UMVerifyHelper mAuthHelper;
  public int mScreenWidthDp;
  public int mScreenHeightDp;

  public static AuthPageConfig init(Constant.UI_TYPE type, Activity activity, UMVerifyHelper authHelper) {
    switch (type) {
      case DIALOG_BOTTOM:
        return new DialogBottomConfig(activity, authHelper);
      default:
        return null;
    }
  }

  public BaseUIConfig(Activity activity, UMVerifyHelper authHelper) {
    mActivity = activity;
    mContext = activity.getApplicationContext();
    mAuthHelper = authHelper;
  }

  protected View initSwitchView(int marginTop) {
    TextView switchTV = new TextView(mContext);
    RelativeLayout.LayoutParams mLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, AppUtil.dp2px(mContext, 50));
    //一键登录按钮默认marginTop 270dp
    mLayoutParams.setMargins(0, AppUtil.dp2px(mContext, marginTop), 0, 0);
    mLayoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
    switchTV.setText(R.string.switch_method);
    switchTV.setTextColor(Color.BLACK);
    switchTV.setTextSize(TypedValue.COMPLEX_UNIT_SP, 13.0F);
    switchTV.setLayoutParams(mLayoutParams);
    return switchTV;
  }

  protected void updateScreenSize(int authPageScreenOrientation) {
    int screenHeightDp = AppUtil.px2dp(mContext, AppUtil.getPhoneHeightPixels(mContext));
    int screenWidthDp = AppUtil.px2dp(mContext, AppUtil.getPhoneWidthPixels(mContext));
    int rotation = mActivity.getWindowManager().getDefaultDisplay().getRotation();
    if (authPageScreenOrientation == ActivityInfo.SCREEN_ORIENTATION_BEHIND) {
      authPageScreenOrientation = mActivity.getRequestedOrientation();
    }
    if (authPageScreenOrientation == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
      || authPageScreenOrientation == ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE
      || authPageScreenOrientation == ActivityInfo.SCREEN_ORIENTATION_USER_LANDSCAPE) {
      rotation = Surface.ROTATION_90;
    } else if (authPageScreenOrientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
      || authPageScreenOrientation == ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT
      || authPageScreenOrientation == ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT) {
      rotation = Surface.ROTATION_180;
    }
    switch (rotation) {
      case Surface.ROTATION_0:
      case Surface.ROTATION_180:
        mScreenWidthDp = screenWidthDp;
        mScreenHeightDp = screenHeightDp;
        break;
      case Surface.ROTATION_90:
      case Surface.ROTATION_270:
        mScreenWidthDp = screenHeightDp;
        mScreenHeightDp = screenWidthDp;
        break;
      default:
        break;
    }
  }


  /**
   *  在横屏APP弹竖屏一键登录页面或者竖屏APP弹横屏授权页时处理特殊逻辑
   *  Android8.0只能启动SCREEN_ORIENTATION_BEHIND模式的Activity
   */
  public void onResume() {

  }

  public void release() {
    mAuthHelper.setAuthListener(null);
    mAuthHelper.setUIClickListener(null);
    mAuthHelper.removeAuthRegisterViewConfig();
    mAuthHelper.removeAuthRegisterXmlConfig();
  }
}
